package com.chenl.provider.DEMOS.Quartz.dynamicQuartz;

import com.alibaba.fastjson.JSONObject;
import com.chenl.provider.DEMOS.Quartz.dynamicQuartz.po.SysJob;
import com.chenl.provider.DEMOS.Quartz.dynamicQuartz.po.SysJobServiceImpl;
import com.chenl.provider.DEMOS.Quartz.dynamicQuartz.util.QuartzUtil;
import com.chenl.provider.DEMOS.Quartz.dynamicQuartz.vo.JobVo;
import com.chenl.provider.DEMOS.Quartz.dynamicQuartz.zenum.ActionEnum;
import com.chenl.provider.DTO.vo.Result;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/quartz")
public class QuartzCon {
    @Autowired
    SysJobServiceImpl sysJobService;

    @PostMapping("/test")
    public String test(@RequestParam Map<String,Object>map, @RequestBody SysJob sysJob){
//        QuartzUtil.startJob(sysJob);
        System.out.println(map);
        System.out.println(JSONObject.toJSONString(sysJob));
        return "okk";
    }

    /**
     * 获取任务列表
     * @return
     */
    @GetMapping("/getJobList")
    public Result<List<SysJob>> getJobList() {
        List<SysJob> list = sysJobService.list();
        return new Result<>(list);
    }

    @PostMapping("/flushJobs")
    public Result<List<SysJob>> flushJobs() {
        List<SysJob> list = sysJobService.list();
        for (SysJob sysJob : list) {
            JobVo jobVo =new JobVo();
            jobVo.setJobId(sysJob.getId());
            jobVo.setJobGroup(sysJob.getJobGroup());
            Boolean flag = (Boolean) QuartzUtil.doJob(jobVo, ActionEnum.check);
            if (flag &&"0".equals(sysJob.getStatus())){
                log.info("删除任务:{}", JSONObject.toJSONString(sysJob));
                QuartzUtil.doJob(jobVo, ActionEnum.delete);
            }else if (!flag && "1".equals(sysJob.getStatus())){
                log.info("开启任务:{}", JSONObject.toJSONString(sysJob));
                QuartzUtil.startJob(sysJob);
            }
        }
        return new Result<>(list);
    }

    /**
     * 根据ID获取任务
     * @param id
     * @return
     */
    @PostMapping("/getJobById/{id}")
    public Result<SysJob> getJobById(@PathVariable(value = "id") Long id) {
        SysJob sysJob = sysJobService.getById(id);
        return new Result<>(sysJob);
    }

    /**
     * 获取并刷新一个任务
     */
    @SneakyThrows
    @GetMapping("/getJobAndJoin/{id}")
    public Result<SysJob> getJobAndJoin(@PathVariable(value = "id") Integer id) {
        SysJob sysJob = sysJobService.getById(id);
        JobVo jobVo =new JobVo();
        jobVo.setJobId(sysJob.getId());
        jobVo.setJobGroup(sysJob.getJobGroup());
        Boolean flag = (Boolean) QuartzUtil.doJob(jobVo, ActionEnum.check);
        if (flag &&"0".equals(sysJob.getStatus())){
            log.info("删除任务:{}", JSONObject.toJSONString(sysJob));
            QuartzUtil.doJob(jobVo, ActionEnum.delete);
        }else if (!flag && "1".equals(sysJob.getStatus())){
            log.info("开启任务:{}", JSONObject.toJSONString(sysJob));
            QuartzUtil.startJob(sysJob);
        }
        return new Result<>(sysJob);
    }

    /**
     * 新增/修改任务,并加入的调度器中执行
     * @param sysJob
     * @return
     */
    @PostMapping("/saveJob")
    public  Result<Boolean> saveJob(SysJob sysJob) {
        sysJobService.saveOrUpdate(sysJob);
        QuartzUtil.startJob(sysJob);
        return new Result<>();
    }

    /**
     * description="删除任务,并删除调度器中执行的该任务
     * @param id
     * @return
     */
    @PostMapping("/deleteJob/{id}")
    public Result<Boolean> deleteJob(@PathVariable(value = "id") Integer id) {
        SysJob sysJob = sysJobService.getById(id);
        boolean data = this.sysJobService.removeById(id);
        if(sysJob!=null) {
            JobVo jobVo = new JobVo();
            jobVo.setJobId(sysJob.getId());
            jobVo.setJobGroup(sysJob.getJobGroup());
            QuartzUtil.doJob(jobVo, ActionEnum.delete);
        }
        return new Result<>(data);
    }

    /**
     * 手动执行一个任务
     * @param invokeTarget
     * @return
     */
    @PostMapping("/runJob")
    public Result<Boolean> runJob(String invokeTarget) {
        QuartzUtil.runJob(invokeTarget);
        return new Result<>();
    }

    /**
     * 检查任务
     * @param jobVo
     * @return
     */
    @SneakyThrows
    @PostMapping("/checkJob")
    public  Result<Object> checkJob(@RequestBody JobVo jobVo) {
        Boolean flag = (Boolean) QuartzUtil.doJob(jobVo, ActionEnum.check);
        String status = QuartzUtil.getStatus(jobVo);
        return new Result<>(Boolean.TRUE.equals(flag)+"_"+status);
    }

    /**
     * 恢复任务
     * @param sysJob
     * @return
     */
    @PostMapping("/resumeJob")
    public  Result<Boolean> resumeJob(@RequestBody JobVo sysJob) {
        QuartzUtil.doJob(sysJob,ActionEnum.resume);
        return new Result<>();
    }

    /**
     * 暂停任务
     * @param sysJob
     * @return
     */
    @PostMapping("/pauseJob")
    public  Result<Boolean> pauseJob(@RequestBody JobVo sysJob) {
        QuartzUtil.doJob(sysJob,ActionEnum.pause);
        return new Result<>();
    }

}
